let nums = [5, 6, 7, 0, 1, 2, 4],
  target = 0;

function binarySearch(nums, target) {
  let left = 0;
  let right = nums.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] == target) {
      return mid;
    } 
    //左边部分有序
    if (nums[left] <= nums[mid]) {
        if(nums[left] <= target && target < nums[mid]){
            right = mid - 1
        } else {
            left = mid + 1;
        }
    //右边部分有序
    } else {
        if(nums[left] <= target && target < nums[mid]){
            left = mid + 1;
        } else {
            right = mid - 1
            
        }
    }
    return -1;
  }
}

let index = binarySearch(nums, target)
console.log(index)
